package com.boot2551.test;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

interface SumI{
    double sum(double a,double b);
}
class Sum{
    double sum(double a,double b){
        return  a+b;
    }
}
public class P1 {
    public static void main(String[] args) {
        SumI a=(aa,b)->aa+b;
        System.out.println(a.sum(10,20));
        //jdk的动态代理
        SumI b= (SumI) Proxy.newProxyInstance(P1.class.getClassLoader(),new Class[]{
                SumI.class
        },(proxy,method,arg)->{
            System.out.println("方法增强");
            return method.invoke(a,arg[0],arg[1]);
        });
        System.out.println(b.sum(10,20));
        //创建sum的代理对象
        Sum s=new Sum();
        //增强类
        Enhancer e=new Enhancer();
        //设置父类
        e.setSuperclass(s.getClass());
        e.setCallback((MethodInterceptor)(proxy, method, arg,var4)->{
            System.out.println("cglib方法增强");
            return method.invoke(s,arg[0],arg[1]);
        });
        Sum s2=(Sum)e.create();
        System.out.println(s2.sum(10,20));
    }
}
